package top.xcxdl.rural.controller.admin;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import top.xcxdl.rural.common.Res;
import top.xcxdl.rural.entity.RuralCourse;
import top.xcxdl.rural.entity.RuralProduct;
import top.xcxdl.rural.entity.RuralTab;
import top.xcxdl.rural.service.IRuralCourseService;
import top.xcxdl.rural.service.IRuralProductService;
import top.xcxdl.rural.service.IRuralTabService;
import top.xcxdl.rural.utils.JWTUtils;
import top.xcxdl.rural.utils.JwtInterceptor;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ding
 * @since 2022-05-17
 */
@Api("后台管理产品控制器")
@RestController
@RequestMapping("/admin/tab")
public class RuralTabController {

    @Resource
    private IRuralTabService service;

    @Resource
    private IRuralProductService productService;

    @Resource
    private IRuralCourseService courseService;

    /**
     * 后台获取所有轮播
     * @param pageNo 页码
     * @param pageSize 每页数量
     * @param tabType 轮播类型（0地图，1正常产品）
     * @param key 搜索关键字
     * @return 轮播列表
     */
    @ResponseBody
    @GetMapping("/getList")
    @ApiOperation("后台获取所有轮播")
    public Res<Object> getList(
            @RequestParam(name = "page_no") Integer pageNo,
            @RequestParam(name = "page_size") Integer pageSize,
            @RequestParam(name = "tab_type", required = false) Integer tabType,
            @RequestParam(name = "title",required = false) String key
    ) {
        PageInfo<RuralTab> pageInfo = service.getList(pageNo, pageSize, tabType, key);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return Res.success(jsonObject);
    }

    /**
     * 后台新增 or 修改（处理）轮播信息
     * @param tab 轮播实体
     * @return res
     */
    @PostMapping("/doEdit")
    @ApiOperation("后台新增 or 修改（处理）轮播信息")
    @ResponseBody
    public Res<Object> doEdit(
            @RequestBody RuralTab tab){
        Integer isNew = tab.getTabId();
        int ret = 0;
        ret += service.doEdit(tab);
        return ret > 0 ? Res.success(isNew == null ? "添加成功" : "编辑成功") : Res.error("操作失败");
    }

    @ApiOperation("后台删除轮播")
    @PostMapping("/doDelete")
    @ResponseBody
    public Res<Object> delete(@RequestBody JSONObject jsonObject) {
        //获取id集合
        List<Integer> ids = (List<Integer>) jsonObject.get("ids");
        //删除
        int ret = service.delete(ids);
        return ret > 0 ? Res.success("删除成功") : Res.error("删除失败");
    }

    /**
     * 后台获取产品详情
     * @param productId 产品id
     * @return 产品详情实体
     */
    @ApiOperation("后台获取产品详情")
    @GetMapping("/getProductDetail")
    @ResponseBody
    public Res<RuralProduct> getDetail(@RequestParam(name = "product_id") Integer productId) {
        RuralProduct product = productService.getDetail(productId);
        return Res.success(product);
    }

    /**
     * 远程搜索产品列表
     * @param key 搜索键
     * @return 产品列表
     */
    @ApiOperation("远程搜索产品列表")
    @GetMapping("/getProductList")
    @ResponseBody
    public Res<List<RuralProduct>> getProductList(@RequestParam(name = "title") String key) {
        return Res.success(productService.getProductListBySearch(key));
    }

    /**
     * 远程搜索课程列表
     * @param key 搜索键
     * @return 课程列表
     */
    @ApiOperation("远程搜索课程列表")
    @GetMapping("/getCourseList")
    @ResponseBody
    public Res<List<RuralCourse>> getCourseList(@RequestParam(name = "title") String key) {
        return Res.success(courseService.getCourseListBySearch(key));
    }
}
